﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Besture.Bll;
using Besture.Bll.UserBase;
using Besture.Model;
using Besture.Model.XiangSheng;
using Besture.Common;
using Besture.Common.Helper;
using Besture.UI.Common;

namespace Besture.UI.Portal.Pay
{
    public partial class OrderPay : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["OrderID"] != null)
                {
                    GetNoPayOrder(Convert.ToInt32(Request["OrderID"].ToString()));
                }
                //GetNoPayOrder(20);
            }
        }

        private void GetNoPayOrder(int orderid)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel) HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                //OrderDac dac = new OrderDac();
                //OrderPay_Order_Model model = dac.GetNoPayOrder(orderid, ubm.ID);
                OrderDac dac = new OrderDac();
                OrderPay_Order_Model model = dac.GetNoPayOrder(orderid, ubm.ID);
                this.lblHotelName1.Text = model.HotelsName;
                this.lblHotelName2.Text = model.HotelsName;
                this.lblBeginTime.Text = model.CheckDate.ToString("yyyy-MM-dd");
                this.lblEndTime.Text = model.DepartureDate.ToString("yyyy-MM-dd");
                this.lblDay1.Text = (model.DepartureDate - model.CheckDate).Days.ToString() + "晚";
                this.lblRoomName.Text = model.RoomName;
                this.lblPrice.Text = model.CopePrice.ToString("f0");
                this.lblRoomNum.Text = model.RoomQuantity.ToString();
                this.lblUserName.Text = model.ContactName;
                this.lblArrive.Text = model.LatestTime.ToString("HH:mm");
                this.lblNightPrice.Text = model.NightPrice.ToString();
                this.lblOrderNo.Text = model.OrderNo;
                this.lblScorePrice.Text = "0";
                this.lblScore.Text = model.UserPoints.ToString();
                this.lblActuallyPaidPrice.Text = (model.CopePrice - model.NightPrice).ToString("f0");
                this.hfIntegration.Value = model.Integration.ToString();
                this.hfIntegrationRMB.Value = model.IntegrationRMB.ToString();
                Session["PayInfo"] = model;
            }
        }

        /// <summary>
        /// 保存订单
        /// </summary>
        /// <param name="orderNo">订单号</param>
        /// <param name="consumeIntegration">消耗的积分</param>
        /// <param name="deductiblePrice">积分抵扣价</param>
        /// <param name="nightNumberPrice">夜间数抵扣价</param>
        /// <param name="actuallyPaidPrice">实付价</param>
        /// <param name="payMethod">支付方式</param>
        /// <returns></returns>
        [WebMethod]
        public static int SaveOrder(string on, string ci, decimal dp, decimal np, decimal ap, int payMethod)
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] != null)
            {
                UserBaseModel ubm = ((UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]);
                OrderPay_Order_Model model = (OrderPay_Order_Model)HttpContext.Current.Session["PayInfo"];
                int consumeIntegration = (ci == "" ? 0 : Convert.ToInt32(ci));
                model.OrderNo = on;
                model.ConsumeIntegration = consumeIntegration;
                model.DeductiblePrice = dp;
                model.NightNumberPrice = np;
                model.ActuallyPaidPrice = ap;
                model.PayMethod = payMethod;
                OrderDac dac = new OrderDac();
                int result = dac.UpdateOrder(on, consumeIntegration, dp, np, ap, payMethod, ubm.ID);

                if (result > 0)
                { 
                    //201507291530 lyg 添加log日志 start
                    string sLogContent = string.Format("{0}已在{1}支付成功(订单号：{2})",
                        GetUserLoginModel().LoginName, Convert.ToString(DateTime.Now), model.OrderNo);
                    new OrderLogDac().Add(sLogContent, new OrderDac().GetModelByOrderNo(on).ID);
                    //201507291530 lyg 添加log日志 end 
                }
                return result;
            }
            else
            {
                return 0;
            }
        }
    }
}